import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../com/Login.vue'
import Home from '../com/home.vue'
import Welcome from '../com/Welcome.vue'
import Users from '../com/user/User.vue'
import rights from '../com/jurisd/rights.vue'
import roles from '../com/jurisd/roles.vue'
import params from '../com/goods/params.vue'
import goodsList from '../com/goods/goodsList.vue'
import categories from '../com/goods/categories.vue'
import Add from '../com/goods/add.vue'
//导入全局样式
import '../assets/css/global.css'
Vue.use(VueRouter)

const routerObj = new VueRouter({
  routes: [
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        { path: '/welcome', component: Welcome },
        { path: '/users', component: Users },
        { path: '/roles', component: roles },
        { path: '/rights', component: rights },
        { path: '/params', component: params },
        { path: '/goods', component: goodsList },
        { path: '/goods/add', component: Add },
        { path: '/categories', component: categories }
      ]
    },
    { path: '/', redirect: '/login' }
  ]
})
//挂载路由导航守卫

routerObj.beforeEach((to, from, next) => {
  //to 将要访问的路径
  // from 代表从哪个路径跳转过来
  // next 是一个函数，表示放行
  if (to.path === '/login') return next()
  // 获取token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default routerObj
